package com.woniu.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.Atten;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author hyl
 * @since 2022-08-28
 */
public interface AttenMapper extends BaseMapper<Atten> {
    @Select("SELECT atten.*,user.user_name, FROM atten,user WHERE user_id = #{userId} AND atten_date=#{attenDate} AND user")
    Atten getByUserIdAndAttenDate(@Param("userId") Integer userId, @Param("attenDate") String attenDate);

    @Update("<script>                                           "
            + "UPDATE atten                                   "
            + "<set>                                            "
            + "  <if test='endTime != null'> end_time=#{endTime}, </if> "
            + "  <if test='startTime != null'> start_time=#{startTime}, </if> "
            + "  <if test='attenStatusOn != null'> atten_status_on=#{attenStatusOn}, </if> "
            + "  <if test='attenStatusOff != null'> atten_status_off=#{attenStatusOff} </if> "
            + "</set>                                           "
            + "WHERE atten_id = #{attenId}                                 "
            + "</script>                                        ")
    void updateAttendance(Atten atten);

    @Select("SELECT user_id FROM atten WHERE atten_date=#{AttenDate}")
    List<Integer> getUserIdByAttenDate(String AttenDate);

    @Select("SELECT * FROM atten WHERE atten_date =#{date} AND atten_status_off IS null")
    List<Atten> getOffWorkNotAttenByDate(String AttenDate);

    @Select("SELECT atten.*,user.user_name,dept.dept_name FROM atten,user,dept ${ew.customSqlSegment} AND atten.user_id=user.user_id AND user.dept_id=dept.dept_id")
    @Results(id = "AttenMap", value = {
            @Result(column = "dept_name", property = "dept.deptName"),
            @Result(column = "user_name", property = "user.userName"),
    })
    Page<Atten> getAttenWithPage(Page<Atten> page, @Param(Constants.WRAPPER) Wrapper<Atten> wrapper);
}
